package br.ufrgs.dbproject.pathcache;

public class CacheEntry {

	private final Integer key;
	private final Path path;
	private boolean newEntry = true;

	public CacheEntry(Integer key, Path path) {
		
		this.key = key;
		this.path = path;
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if (obj == null) {
			return false;
		}
		
		if (this == obj) {
			return true;
		}
		
		if (obj instanceof CacheEntry) {
			
			CacheEntry that = (CacheEntry)obj;
			return super.equals(obj) ||
					this.key.equals(that.key) && this.path.equals(that.path);
			
		} else if (obj instanceof Path || obj instanceof String) {
			return this.path.equals(obj);
		}
		
		return false;
	}
	
	/**
	 * Marks this entry as fetched.
	 * 
	 * <p>
	 * this will happen when this entry is fetched for the first time.
	 * </p>
	 * 
	 * @return the cache entry itself.
	 */
	public CacheEntry markAsFetched() {
		newEntry = false;
		return this;
	}
	
	/**
	 * Check whether this entry was fetched or not.
	 * 
	 * @return true if it was already fetched, false otherwise.
	 */
	public boolean wasFetched() {
		return newEntry;
	}
	
	@Override
	public String toString() {
		
		return "[" + key + ", " + path + "]";
	}

	public Integer getKey() {
		return key;
	}

	public Path getPath() {
		return path;
	}
}
